/**
 * Copyright 2011 Joe Choi, All rights reserved.
 */
package tec.fs;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import tao.data.structure.tree.ConcreteTrees;
import tao.data.structure.tree.Tree;
import tao.data.structure.tree.Trees;
import tao.file.FileTree;

/**
 * @author Joe Choi
 */
public class FileSubtree {
	public FileSubtree(File parent) {
		this.parent = parent;
		treeList = null;
	}
	
	public Trees<File> toTrees() {
		if(treeList == null) load();
		return(new ConcreteTrees<File>(treeList));
	}
	
	public void add(FileTree node) {
		if(treeList == null) load();
		treeList.add(node);
	}
	
	private void load() {
		List<File> fileList = new ArrayList<File>();
		
		if(parent.isDirectory()) {
			File[] files = parent.listFiles();
			Arrays.sort(files);
			fileList = Arrays.asList(files);
		}
		for(File f : fileList) treeList.add(new FileTree(f));
	}
	
	private File parent;
	private List<Tree<File>> treeList;
}